package com.ruipengkj.view.memberView;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.bean.TShopCharge;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.JPanelGridLayoutComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.RPJButtonRoundBlue;
import com.ruipengkj.component.RPJButtonRoundOrager;
import com.ruipengkj.component.jpanel.QueryPanel;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.MemberQueryShopChargeModel;
import com.ruipengkj.service.TShopChargeService;
import com.ruipengkj.view.base.BasePagePanel;
//集成分页组件
public class ShopChargePanel extends BasePagePanel<TShopCharge> implements Runnable{
		private static final long serialVersionUID = 1L;
		//private JLabelJTextFieldComponent cardNumTF = null;//发卡时间
		private DataPackageComponent startTimeTF = null;//发卡时间
		private DataPackageComponent timeTF = null;//时间
		
		
		private JButton queryButton = null;//查询按钮
		private JButton cleateButton = null;//清空按钮
		private JButton addButton = null;//添加按钮
		private JButton deleteButton = null;//删除按钮
		
		
		private JTable table;
		private MemberQueryShopChargeModel memberQueryShopChargeModel;
		private PagingComponent pagingComponent;//翻页组件
		private PageView<TShopCharge> pageView;//翻页主键
		private JScrollPane tableJsp;
		private TShopChargeService tShopChargeService;
		private MemberShopChargeAdd memberShopChargeAdd;//会员详细窗体
		private MemberShopChargeUpdate memberShopChargeUpdate;//会员详细窗体
		public ShopChargePanel() {
			tShopChargeService=BuildSpringApplication.getInstance().getTShopChargeServiceImpl();
			this.setLayout(new BorderLayout());
			this.init();
			this.initListener();
		}
		private void initListener() {
			// TODO Auto-generated method stub
			queryButton.addActionListener(this);
			cleateButton.addActionListener(this);
			deleteButton.addActionListener(this);
			addButton.addActionListener(this);
		}
		private void init() {
			// TODO Auto-generated method stub
			pageView=new PageView<TShopCharge>();
			memberQueryShopChargeModel=new MemberQueryShopChargeModel();
			table=uiFactory.createRPJTable(memberQueryShopChargeModel, null, this, 30, JLabel.CENTER);
			//table设置列宽度 AUTO_RESIZE_SUBSEQUENT_COLUMNS 填满父容器  JTable.AUTO_RESIZE_OFF 自适应
			table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
			tableJsp=uiFactory.createJScrollPane(table);
			pagingComponent=new PagingComponent();
			pagingComponent.setPageDelegale(this);
			
			this.add(this.topPanel(), BorderLayout.NORTH);
			this.add(tableJsp, BorderLayout.CENTER);
			this.add(pagingComponent, BorderLayout.SOUTH);
			this.add(this.initTail(), BorderLayout.SOUTH);
			
			this.termQuery(1);
			//排序
			TableRowSorter<MemberQueryShopChargeModel> tableRowSorter = new TableRowSorter<MemberQueryShopChargeModel>(memberQueryShopChargeModel);  
			table.setRowSorter(tableRowSorter);
		}
		private JPanel topPanel() {
			QueryPanel queryPanel=new QueryPanel("充值规则管理",ImageFactory.loadImage(ImageFactory.FUN_7_ICON_1));
			JPanel jp=new JPanel(new FlowLayout(FlowLayout.LEFT));
			//cardNumTF = new JLabelJTextFieldComponent("卡号：", 15);
			startTimeTF = new DataPackageComponent("发卡时间：", 15);
			timeTF = new DataPackageComponent("时间：", 15);
			queryButton = new RPJButtonBlue("查询");
			cleateButton= new RPJButtonGray("清空");
			//jp.add(cardNumTF);
			jp.add(startTimeTF);
			jp.add(timeTF);
			jp.add(queryButton);
			jp.add(cleateButton);
			queryPanel.addPanel(jp);
			return queryPanel;
		}
		private JPanel initTail() {
			addButton = new RPJButtonRoundBlue("添加");
			deleteButton = new RPJButtonRoundOrager("删除");
			JPanel jp2 = new JPanelFlowLayoutComponent(new Component[]{addButton,deleteButton}, FlowLayout.LEFT);
			JPanel tail_panel = new JPanelGridLayoutComponent(new Component[]{pagingComponent,jp2}, 2, 1);
			return tail_panel;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			this.termQuery(1);
		}
		//条件查询
		protected void termQuery(final int currentPage) {
			final TShopCharge tshopcharge=new TShopCharge();
			//tshopcharge.setChargeID(cardNumTF.getText().getText().trim());
			tshopcharge.setStartTime(startTimeTF.getDate());
			tshopcharge.setEndTime(timeTF.getDate());
			new Thread(){
				public void run(){
					tableJsp.getViewport().remove(table);
					tableJsp.getViewport().add(waitComponent);
					try {
						pageView.setCurrentPage(currentPage);
						PageModel<TShopCharge> pageModel=tShopChargeService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tshopcharge);
						pageView.setPageModel(pageModel);
						pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
						if(pageModel!=null){
							Util.clearTableRowSorter(table);
							memberQueryShopChargeModel.setRowData(pageModel.getResultList());
							SwingUtilities.invokeLater(new Runnable() {
								@Override
								public void run() {
									tableJsp.getViewport().remove(waitComponent);
									tableJsp.getViewport().add(table);
//									Util.setJTabelAutoClumnWidth(table);
									pagingComponent.show();
								}
							});
						}else{
							//没有优惠卷信息
							tableJsp.getViewport().remove(waitComponent);
							tableJsp.getViewport().add(table);
							memberQueryShopChargeModel.clear();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}.start();
		}
		@Override
		protected PageView<TShopCharge> getPageView() {
			return pageView;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==queryButton){
				
				//查询
				this.termQuery(1);
				
			}else if(e.getSource()==addButton){
				//添加
				memberShopChargeAdd=new MemberShopChargeAdd(null, "添加充值规则", true);
				memberShopChargeAdd.showDialog();
				if(memberShopChargeAdd.isSuccess){
					this.termQuery(1);
				}
				
			}else if(e.getSource()==cleateButton){
				//清除
				//cardNumTF.getText().setText("");
				startTimeTF.setDate(null);
				timeTF.setDate(null);
			}else if(e.getSource()==deleteButton){
				int row = table.getSelectedRow();
				if (row != -1) {
					if (Util.showConfirmDialog("是否要删除选择记录") == 0) {
						String id=table.getValueAt(table.getSelectedRow(), -1).toString();
						try {
							tShopChargeService.deleteService(id);
							JOptionPane.showMessageDialog(null, "删除成功");
							this.termQuery(1);
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							JOptionPane.showMessageDialog(null, "删除失败（请确定商品未被其他数据库表使用）");
						}
					}
				}else{
					JOptionPane.showMessageDialog(null, "请选择行在进行删除");
					return;
				}
			}
		}
		@Override
		public void keyReleased(KeyEvent e) {
			if(e.getKeyCode()==KeyEvent.VK_ENTER){
				if(e.getSource()==startTimeTF.getDate()||e.getSource() == timeTF.getDate()){
					//查询会员
					queryButton.doClick();
				}
			}
		}
		// 鼠标双击 显示详细
		 
		@Override
		public void mouseClicked(MouseEvent e) {
	    	if(e.getSource()==table&&e.getClickCount() == 2){
	    		//表格
	    		String id=table.getValueAt(table.getSelectedRow(), -1).toString();
	    		try {
					TShopCharge tshopcharge=tShopChargeService.getByIdService(id);
					if(tshopcharge==null){
						JOptionPane.showMessageDialog(null, "当前会员不存在");
						return;
					}
					memberShopChargeUpdate=new MemberShopChargeUpdate(null, "修改页面", true);
					memberShopChargeUpdate.showDialog(tshopcharge);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
	    	}
	    }
		@Override
		public void clear() {
			// TODO Auto-generated method stub
			
		}
}
